package com.cy.pj.sys.web.controller;

import com.cy.pj.common.util.PagUtil;
import com.cy.pj.sys.pojo.SysNotice;
import com.cy.pj.sys.service.SysNoticeService;
import com.cy.pj.sys.web.pojo.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

/**
 * @DATE: 2021/3/10 17:13
 * @Author: LuckyH
 */

// 这个对象是公告模块控制层处理对象
    // 条件查询 （请求方式，URL）

@RestController
@RefreshScope // 配置动态刷新
@RequestMapping("/notice/")
public class SysNoticeController {
    @Autowired
    private SysNoticeService sysNoticeService;

    // 请求URL 80端口可以省略不写
    @GetMapping
    public JsonResult doFindNotices(SysNotice notice){
//        try {
//            sysNoticeService.findNotices(notice);
//        }catch (Exception e){
//            return new JsonResult(e.getMessage());
//        }


        // 插件的使用 分页查询
//        PageInfo<Object> pageInfo =
//                PageHelper.startPage(1, 3).doSelectPageInfo(
//                new ISelect() {
//                    @Override
//                    public void doSelect() {
//                        sysNoticeService.findNotices(notice);
//                    }
//                }
//        );
//        return new JsonResult(pageInfo);

        return new JsonResult(PagUtil.startPage().doSelectPageInfo(()->sysNoticeService.findNotices(notice)));
    }

    @GetMapping("{id}")
    public JsonResult doFindById(@PathVariable Long id){
        return new JsonResult(sysNoticeService.findById(id));
    }


    @DeleteMapping("{id}")
    public JsonResult doDeleteById(@PathVariable Long... id){
        try {
            sysNoticeService.deleteById(id);
        }catch (Exception e){
            return new JsonResult(e);
        }
        return new JsonResult("delete ok");
    }

    @PostMapping
    public JsonResult doSaveNotice(@RequestBody SysNotice notice){


        sysNoticeService.saveNotice(notice);
        return new JsonResult("save ok");
    }

    @PutMapping
    public JsonResult doUpdateNotice(@RequestBody SysNotice notice){
        sysNoticeService.updateNotice(notice);
        return new JsonResult("update ok");
    }


}
